package com.mchsdk.paysdk.activity.fragments;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mchsdk.paysdk.activity.ToCertificateActivity;
import com.mchsdk.paysdk.bean.PersonalCenterModel;
import com.mchsdk.paysdk.common.Constant;
import com.mchsdk.paysdk.http.process.CertificateProcess;
import com.mchsdk.paysdk.utils.MCHInflaterUtils;
import com.mchsdk.paysdk.utils.MCLog;
import com.mchsdk.paysdk.utils.TextUtils;
import com.mchsdk.paysdk.utils.ToastUtil;

/**
 * Created by LeBron on 2017/5/4.
 */

public class ToCertificateNoFragment extends Fragment {

    private final static String TAG = "ToCertificateNoFragment";
    private View inflate;
    private FragmentActivity activity;
    private EditText editName, editID;//姓名 身份证号码
    private Button mBtn;//
    private String mNameStr, idStr;//姓名 身份证号码
    private LinearLayout mWrong;
    private TextView textWrong;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        inflate = inflater.inflate(MCHInflaterUtils.getLayout(getActivity(), "to_certificate_no_fragment"), null);

        activity = getActivity();
        initview(inflate);

        return inflate;
    }

    private void initview(View inflate) {
        editName = (EditText) inflate.findViewById(MCHInflaterUtils
                .getControl(activity, "mch_et_toCertificate_name"));
        mWrong = (LinearLayout) inflate.findViewById(MCHInflaterUtils
                .getControl(activity, "ll_certificate_wrong"));
        textWrong = (TextView) inflate.findViewById(MCHInflaterUtils
                .getControl(activity, "text_certificate_wrong"));
        editName.addTextChangedListener(textWatcher);
        editID = (EditText) inflate.findViewById(MCHInflaterUtils
                .getControl(activity, "mch_et_toCertificate_card"));
        editID.addTextChangedListener(textWatcher);
        mBtn = (Button) inflate.findViewById(MCHInflaterUtils
                .getControl(activity, "mch_attestation_button"));
        mBtn.setOnClickListener(attestationoClick);
    }

    /**
     * 请求返回
     */
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constant.To_Certificate_SUCCESS:
                    MCLog.e(TAG, (String) msg.obj);
                    String idcard = (String) msg.obj;
                    PersonalCenterModel.getInstance().channelAndGame.setIdcard(idcard);
                    ToCertificateActivity activity = (ToCertificateActivity) getActivity();
                    activity.showFragment(1);
                    break;
                case Constant.To_Certificate_FAIL:
                    mWrong.setVisibility(View.VISIBLE);
//                   ToastUtil.show(getActivity(),(String) msg.obj);
                    textWrong.setText((String) msg.obj);
                    break;
            }
        }
    };
    /**
     * 去认证
     */
    View.OnClickListener attestationoClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isTure()) {
                goPostReal();
            }
        }
    };

    /**
     * 请求实名认证
     */
    private void goPostReal() {
        CertificateProcess process = new CertificateProcess();
        process.setIdcard(idStr);
        process.setReal_name(mNameStr);
        process.post(mHandler);
    }

    /**
     * 验证姓名  身份证号是否正确
     *
     * @return
     */
    private boolean isTure() {
        mNameStr = editName.getText().toString();
        idStr = editID.getText().toString().trim();
        if (TextUtils.isEmpty(mNameStr)) {
            ToastUtil.show(activity, "请输入姓名");
            return false;
        }
        if (!mNameStr.matches(Constant.REGULAR_NAME)) {
            ToastUtil.show(activity, "请输入正确的姓名");
            return false;
        }
        if (TextUtils.isEmpty(idStr)) {
            ToastUtil.show(activity, "请输入身份证号");
            return false;
        }
        if (idStr.length() == 15) {
            if (!idStr.matches(Constant.REGEX_ID_CARD15)) {
                ToastUtil.show(activity, "请输入正确的身份证号");
                return false;
            }
            return true;
        } else if (idStr.length() == 18) {
            if (!idStr.matches(Constant.REGEX_ID_CARD18)) {
                ToastUtil.show(activity, "请输入正确的身份证号");
                return false;
            }
            return true;
        } else {
            ToastUtil.show(activity, "请输入正确的身份证号");
            return false;
        }
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mWrong.setVisibility(View.INVISIBLE);
            if (editID.getText().length() != 0 && editName.getText().length() != 0) {
                mBtn.setBackgroundResource(MCHInflaterUtils.getDrawable(activity, "mch_button_selector_green"));
                mBtn.setTextColor(Color.parseColor("#F0F0F0"));
            } else {
                mBtn.setBackgroundResource(MCHInflaterUtils.getDrawable(activity, "mch_tocerticate_btn_bg2"));
                mBtn.setTextColor(Color.parseColor("#BEBEBE"));
            }
        }


        @Override
        public void afterTextChanged(Editable s) {
        }
    };
}
